package com.kye.express.business.vipshop;

import com.afollestad.materialdialogs.MaterialDialog;
import com.kye.express.business.vipshop.model.VipExchangeMoneyModel;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.vip.RequestVipExchangableMoney;
import com.kye.model.bean.vip.RequestVipExchange;
import com.kye.model.bean.vip.VipExchangableMoneyBean;
import com.kye.model.bean.vip.VipExchangeResultBean;
import com.kye.model.service.VipService;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 09/02/2018
 * @updated : 09/02/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class VipExchangeConstract {

    public interface VipExchangeView extends IView, LoadingView, ErrorView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void getVipExchangbleMoneySuccess(String money);

        void getVipExchangbleMoneyServerFailed(String msg);

        void getVipExchangbleMoneyNetFailed(String message);

        void exchangeSuccess(VipExchangeResultBean vipExchangeResultBean, MaterialDialog mDialog);

        void exchangeServerFailed(String msg);

        void exchangeNetFailed(String message);
    }

    public static class VipExchangePresenter extends BasePresenter<VipExchangeConstract.VipExchangeView> {

        private VipService vipService;

        public VipExchangePresenter() {
            vipService = RetrofitManager.getInstance().createOpenApiService(VipService.class);
        }

        @Override
        public void bind(VipExchangeView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        /**
         * 获取可兑换金额
         * @param userInfo
         */
        public void getVipExchangableMoney(User userInfo) {
            Single.just(new RequestVipExchangableMoney())
                    .doOnSuccess(new Consumer<RequestVipExchangableMoney>() {
                        @Override
                        public void accept(RequestVipExchangableMoney requestVipExchangableMoney) throws Exception {
                            requestVipExchangableMoney.setPhone(userInfo.getPhone());
                        }
                    })
                    .flatMap(new Function<RequestVipExchangableMoney, SingleSource<OpenApiResult<VipExchangableMoneyBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<VipExchangableMoneyBean>> apply(RequestVipExchangableMoney requestVipExchangableMoney) throws Exception {
                            return vipService.getVipExchangableMoney(requestVipExchangableMoney)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<VipExchangableMoneyBean>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<VipExchangableMoneyBean> result, VipExchangableMoneyBean vipExchangableMoneyBean) {
                            getView().getVipExchangbleMoneySuccess(vipExchangableMoneyBean.getMoney());
                        }

                        @Override
                        public void onApiError(OpenApiResult<VipExchangableMoneyBean> result, int code, String msg) {
                            getView().getVipExchangbleMoneyServerFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getVipExchangbleMoneyNetFailed(e.getMessage());
                        }
                    });
        }

        /**
         * 兑换
         * @param userInfo
         * @param selectedExchangeType
         * @param money
         */
        public void exchange(User userInfo, VipExchangeMoneyModel selectedExchangeType, String money, String smsCodeInput, MaterialDialog mDialog) {
            Single.just(new RequestVipExchange())
                    .doOnSuccess(new Consumer<RequestVipExchange>() {
                        @Override
                        public void accept(RequestVipExchange requestVipExchange) throws Exception {
                            requestVipExchange.setConfirmCode(smsCodeInput);
                            requestVipExchange.setMoneyType(selectedExchangeType.getMoneyType());
                            requestVipExchange.setPhone(userInfo.getPhone());
                            requestVipExchange.setType("2");
                        }
                    })
                    .flatMap(new Function<RequestVipExchange, SingleSource<OpenApiResult<VipExchangeResultBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<VipExchangeResultBean>> apply(RequestVipExchange requestVipExchange) throws Exception {
                            return vipService.exchange(requestVipExchange)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<VipExchangeResultBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<VipExchangeResultBean> result, VipExchangeResultBean vipExchangeResultBean) {
                            getView().exchangeSuccess(vipExchangeResultBean, mDialog);
                        }

                        @Override
                        public void onApiError(OpenApiResult<VipExchangeResultBean> result, int code, String msg) {
                            getView().exchangeServerFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().exchangeNetFailed(e.getMessage());
                        }
                    });
        }
    }

}
